package gui;





import flexibility.userData;
import guiCode.managerCode;

import internalMail.createMailBox;
import internalMail.readMail;

import javax.swing.JFrame;
import java.awt.BorderLayout;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.SpringLayout;

import java.awt.Rectangle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.border.LineBorder;

import secretary.closeSecretaryScreen;
import secretary.defineSemester;

import java.awt.Color;
import java.awt.Toolkit;
import java.awt.Font;
import basic.ClientConsole;
import java.awt.SystemColor;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JLabel;

import com.sun.rowset.CachedRowSetImpl;

public class ManagerGUI extends JFrame {
	
	
	
	private managerCode mac;
	private ReportGUI report;
	private viewRequestGUI viewRequest;
	private BlockAccessGUI blockAccess;
	private JPanel FirstPanel = null;
	private JButton btnBlockUser = null;
	private JButton btnLogoutSystem;
	private JButton btnRequest=null;
	private JButton btnReport=null;
	private closeSecretaryScreen close =new closeSecretaryScreen();
	private userData userData= new userData();
	public createMailBox CMB = new createMailBox();
	private CachedRowSetImpl crs;
	public defineSemester semester=new defineSemester();
	private readMail RM = new readMail();
	/**
	 * This method initializes btnBlockUser	
	 */
	private JButton getbtnBlockUser() {
		if (btnBlockUser == null) {
			btnBlockUser = new JButton();
			
			btnBlockUser.setFont(new Font("Tahoma", Font.PLAIN, 11));
			btnBlockUser.setBackground(SystemColor.activeCaption);
			btnBlockUser.setBounds(230, 112, 172, 35);
			btnBlockUser.setText("Block Access To SudentFile");
			 btnBlockUser.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					btnLogoutSystem.setVisible(false);
					btnBlockUser.setVisible(false);
					setContentPane(blockAccess);
				}
			});
		}
		return btnBlockUser;
	}
	
	private JButton getBtnLogoutSystem() {
		if (btnLogoutSystem == null) {
			btnLogoutSystem = new JButton("Logout System");
			btnLogoutSystem.setBackground(SystemColor.activeCaption);
			btnLogoutSystem.setBounds(274, 313, 128, 35);
		}
		return btnLogoutSystem;
	}
	
	private JButton getbtnRequest() {
		
			btnRequest = new JButton("View Request");
			btnRequest.setBackground(SystemColor.activeCaption);
			btnRequest.setBounds(132, 176, 172, 35);
			btnRequest.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					CMB.setConnection(mac.getConnection());
					CMB.setUserID(mac.myId);
					CMB.setJDBC(ManagerGUI.this.crs);
					CMB.setSemesterID(semester.identify(0, mac.getConnection(), crs));
					RM.getMyMails(CMB, mac.getConnection(), crs, mac.myId);
					
				}
			});
		
			return btnRequest;
		}
	private JButton getbtnReport() {
		if (btnReport == null) {
			btnReport = new JButton("Report");
			btnReport.setBackground(SystemColor.activeCaption);
			btnReport.setBounds(10, 112, 172, 35);
			btnReport.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					setContentPane(report);
				}
			});
			
		}
		return btnReport;
	}
	
	/**
	 * This is the default constructor
	 */
	public ManagerGUI() {
		super();
		setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		initialize();
	}
	public ManagerGUI(ClientConsole conn, Integer whoAmI) {

	   super();
	   setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
	    mac=new managerCode(); 
	    this.setConnection(conn);
	    this.setID(whoAmI);
		initialize();
		

		
	}
	/**
	 * This method initializes AcademicFrame 
	 */

	private void initialize() {
		
		this.setResizable(false);
		this.setSize(455, 396);
		this.setLocation(200,200);
		this.setContentPane(getFirstPanel());
		this.setTitle("Manager Access Screen");
		
		this.blockAccess=new BlockAccessGUI(800, 395,  mac.getConnection());
		this.report=new ReportGUI(580, 395, mac.getConnection() );
		this.viewRequest=new viewRequestGUI (800, 395, mac.getConnection());
		
		this.setVisible( true ); 
		
		blockAccess.btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				btnLogoutSystem.setVisible(true);
				btnBlockUser.setVisible(true);
				setContentPane(FirstPanel);
				
				
			}
		});
		report.btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setContentPane(FirstPanel);
			}
		});
		viewRequest.btnBack.addActionListener(new ActionListener() {
		   	public void actionPerformed(ActionEvent arg0) {
		   		setContentPane(FirstPanel);
		   	}
		   });
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent arg0) {
				int n = JOptionPane.showConfirmDialog(
					    null,
					    "You are About Logout The System, are you sure you want to exit the program?",
					    "Log out",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
				if (n==JOptionPane.NO_OPTION ){
					return; 
				}
				close.closeSecretearyScreen(mac.myId,mac.getConnection(),userData);
			}
		});
		btnLogoutSystem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				int n = JOptionPane.showConfirmDialog(
					    null,
					    "You are About Logout The System, are you sure you want to exit the program?",
					    "Log out",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE);
				if (n==JOptionPane.NO_OPTION ){
					return; 
				}
				close.closeSecretearyScreen(mac.myId,mac.getConnection(),userData);
			}
		});
	}
	
	/**
	 * This method initializes FirstPanel
	 */
	private JPanel getFirstPanel() {
		if (FirstPanel == null) {
			FirstPanel = new JPanel();
			FirstPanel.setBackground(new Color(204, 255, 255));
			FirstPanel.setForeground(SystemColor.activeCaption);
			FirstPanel.setLayout(null);
			FirstPanel.add(getbtnBlockUser());
			FirstPanel.add(getbtnRequest());
			FirstPanel.add(getbtnReport());
			FirstPanel.add(getBtnLogoutSystem());
			
			JLabel lblPleaseChooseThe = new JLabel("Please Choose the required option:");
			lblPleaseChooseThe.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 16));
			lblPleaseChooseThe.setBounds(10, 22, 319, 42);
			FirstPanel.add(lblPleaseChooseThe);
			
			
		
		}
		return FirstPanel; 
	}
	
	void setID(Integer whoAmI){
		mac.setID(whoAmI);
	}
	void setConnection(ClientConsole conn){
		
		
		mac.setConnection(conn);
	}
}
	
